package com.heima.search.service.Impl;

import com.heima.common.dto.ResponseResult;
import com.heima.search.dto.ApArticleSearchDto;
import com.heima.search.entity.ApAssociateWords;
import com.heima.search.search.ApArticleSearchRepository;
import com.heima.search.search.ApAssociateWordsRepository;
import com.heima.search.service.IArticleAssociateService;
import javassist.runtime.Desc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
@Service
public class ArticleAssociateServiceImpl implements IArticleAssociateService {
    @Autowired
    private ApAssociateWordsRepository apAssociateWordsRepository;

    /**
     * 联想搜索分页查询
     * @param dto
     * @return
     */
    @Override
    public ResponseResult<List<ApAssociateWords>> associate(ApArticleSearchDto dto) {
//        校验数据
        if (StringUtils.isEmpty(dto.getSearchWords()) || StringUtils.isEmpty(dto.getSearchWords().trim())){
            return ResponseResult.okResult();
        }

        Pageable page = PageRequest.of(0,dto.getSize(), Sort.Direction.DESC,"createdTime");
        List<ApAssociateWords> words = apAssociateWordsRepository.findTopByAssociateWordsStartingWith(dto.getSearchWords(), page);
        return ResponseResult.okResult(words);
    }
}
